<?php
namespace App\controller;
use Core\Db; 
use App\model\logModel;
use App\model\userModel;
use App\model\ocrModel;
use Slim\Http\UploadedFile;
//$_SESSION['user']=array('uid'=>1000,'name'=>'无名');
class bbxController
{ 
	private $topic_arr=array(1=>"地点介绍",2=>"兴趣爱好",3=>"日常生活",4=>"校园生活",5=>"计划愿望",6=>"文化节日",7=>"自然与环境",8=>"社会热点",9=>"人物介绍",10=>'旅游交通',11=>'文化宗教节日',12=>'卫生健康',13=>'文学艺术',14=>'语言学习',15=>'现代科技',16=>'文娱体育',17=>'情感态度');
	private $tip_arr=array(1=>"初中高分范文",2=>"高中高分范文",3=>"大学高分范文");
	private $genre_arr=array(1=>"说明文",2=>"记叙文",3=>"议论文",4=>"应用文",5=>"其他",6=>"概要写作",7=>'读后续写'); 
	public function getOcr($request,$response,$args){
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($response,404,'请先登录！'); 
		}
		$uploadedFiles = $request->getUploadedFiles(); 
	
		$uploadedFile = @$uploadedFiles['picture'];
		if(empty($uploadedFile) || $uploadedFile->getError() != UPLOAD_ERR_OK) { 
			return jsonplay($response,505,'识别不到图片文件！');  
		} 
	  if($uploadedFile->getSize()>3024000){
		  return jsonplay($response,306,'图片太大了');
	  }
	 $ocr=new ocrModel();
	  $cfile=$uploadedFile;//ROOT_PATH.'/static/20191012112309.png';  
	  $targetDir=ROOT_PATH.'/public/upload';
	  $filename=$this->moveUploadedFile($targetDir,$uploadedFile);   
	  $ocr->resizeImage($targetDir.'/'.$filename,800,800,$targetDir.'/newfile','2.jpeg');
	  $cfile = curl_file_create($targetDir.'/newfile2.jpeg', 'image/jpeg'); 
	
	/*
	 
	 $cfile=ROOT_PATH.'/public/static/20191012112309.jpg';
	 $new_file=ROOT_PATH.'/public/static/20191012112309g';
	 $ocr->resizeImage($cfile,800,800,$new_file,'2.jpeg');
	 $cfile = curl_file_create($new_file.'2.jpeg', 'image/jpeg'); 
	   */
	    
	  $word=[];	  
		$text=$ocr->ocrPic($cfile); 
		if($text['code']==0){
			$r=explode('\n',$text['text']);
			foreach($r as $k=>$v){
				$word[]=array('words'=>$v);
			}  
			$code=100;
		}else{
			$code=$text['code'];
		}
		$msg=$text['msg'];
		@unlink($targetDir.'/'.$filename);
		return jsonplay($response,$code,$msg,$word);  
	}
	/**
	* 判断登录
	*/
	public function is_login($request,$res,$args){
		$data_req = $request->getParsedBody();
		
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		}else{
			return jsonplay($res,100); 
		}
	}
	/**
	* 查重检测
	*/
	public function similarity($request,$res,$args){
		$data_req = $request->getParsedBody();
		
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		}
		//验证数据 
		if(check(@$data_req['doc'],'required')){
			return jsonplay($res,401,'句子不能为空'); 
		} 
		Db::table()->insert("bbx_essay", [
			"content" => $data_req['doc'],
			"uid" => $user['uid'],
			"ctime" => time()
		]);
		$eid = Db::table()->id(); 
		 //推送分析
		require "AccessTokenClass.php";
		$Token = new \token();
		$result=$Token->push_similarity($data_req['doc']); 
		if ($result['error_code']==0) {
			$key=$result['data']['key'];
			$data_a['key']=$key;
			$data_a['utime']=time();
			Db::table()->update("bbx_essay", [
				"req_id" => $key,
				"utime" => time()
			],['id'=>$eid]); 
		} 
		//记录数据
		$logModel=new logModel();
		$logModel->add(4,$eid); 
		return jsonplay($res,100,'',['eid'=>$eid]); 
	}
	/**
	* 获取查重结果
	*/
	public function get_similarity($request,$res,$args){
		$eid = $args['eid'];
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		} 
		//验证数据 
		if(check(@$eid,'int')){
			return jsonplay($res,400,'缺少参数'); 
		}
	 	$req_id = Db::table()->get("bbx_essay", ["req_id","content"], [
			"id" => $eid
		]);
		$con=stripslashes($req_id['content']); 
		$con_arr=preg_split("/\n/",$con);
		$data_essay=array();
		foreach($con_arr as $k=>$v){
			if(!empty(trim($v))){
				$data_essay[$k]=essay_cut($v);
			}
		}   
		if(empty($req_id)){
			return jsonplay($res,405,'找不到结果'); 
		}else{
			$result = Db::table()->get("bbx_similarity", "result", [
			"req_id" => $req_id['req_id']
			]);
			if(empty($result)) return jsonplay($res,101,'找不到分析结果'); 
		}
		$data_res=array(
			'essay'=>$data_essay,
			'result'=>json_decode($result)
		); 
		return jsonplay($res,100,'',$data_res); 
		
	}
	/**
	* 相似表达
	*/
	public function parasent($request,$res,$args){
		$data_req = $request->getParsedBody();
		
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		}
		//验证数据 
		if(check(@$data_req['doc'],'required')){
			return jsonplay($res,401,'句子不能为空'); 
		}
		
		Db::table()->insert("bbx_phrase", [
			"content" => $data_req['doc'],
			"uid" => $user['uid'],
			"ctime" => time()
		]); 
		$eid = Db::table()->id(); 
		 //推送分析
		require "AccessTokenClass.php";
		$Token = new \token();
		$result=$Token->push_parasent($data_req['doc']); 
		if ($result['error_code']==0) {
			$key=$result['data']['key'];
			$data_a['key']=$key;
			$data_a['utime']=time();
			Db::table()->update("bbx_phrase", [
				"req_id" => $key,
				"utime" => time()
			],['id'=>$eid]); 
		}
		
		//记录数据
		$logModel=new logModel();
		$logModel->add(5,$eid); 
		return jsonplay($res,100,'',['eid'=>$eid]);  
	}
	/**
	* 获取相似表达结果
	*/
	public function get_parasent($request,$res,$args){
		$eid = $args['eid'];
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		} 
		//验证数据 
		if(check(@$eid,'int')){
			return jsonplay($res,400,'缺少参数'); 
		}
	 	$req_id = Db::table()->get("bbx_phrase", "req_id", [
			"id" => $eid
		]);
		if(empty($req_id)){
			return jsonplay($res,405,'找不到结果'); 
		}else{
			$result = Db::table()->get("bbx_parasent", "result", [
			"req_id" => $req_id
			]);
			if(empty($result)) return jsonplay($res,101,'找不到分析结果'); 
		}
		
		return jsonplay($res,100,'',json_decode($result)); 
		
	}
	/**
	* 自动续写
	*/
	public function autowrite($request,$res,$args){
		$data_req = $request->getParsedBody();
		
		
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		}
		//验证数据 
		if(check(@$data_req['doc'],'required')){
			return jsonplay($res,401,'句子不能为空'); 
		} 
		Db::table()->insert("bbx_essay", [
			"content" => $data_req['doc'],
			"uid" => $user['uid'],
			"ctime" => time()
		]); 
		$eid = Db::table()->id(); 
		 //推送分析
		require "AccessTokenClass.php";
		$Token = new \token();
		$result=$Token->push_autowrite($data_req['doc']); 
		//var_dump($result);
		if ($result['error_code']==0) {
			$key=$result['data']['key'];
			$data_a['key']=$key;
			$data_a['utime']=time();
			Db::table()->update("bbx_essay", [
				"req_id" => $key,
				"utime" => time()
			],['id'=>$eid]); 
		} 
		//记录数据
		$logModel=new logModel();
		$logModel->add(7,$eid); 
		return jsonplay($res,100,'',['eid'=>$eid]);   
	
	}
	/**
	* 自动续写
	*/
	public function autowrite_new($request,$res,$args){
		$data_req = $request->getParsedBody();
		
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		}
		//验证数据 
		if(check(@$data_req['doc'],'required')){
			return jsonplay($res,401,'句子不能为空'); 
		} 
		$url='http://ftp.werror.com:7096/auto/autowrite?snt='.urlencode($data_req['doc']).'&maxlen=30&do_sample=true';
		$return_auto=doCurl($url);
		$content=trim($return_auto,"\"");
		return jsonplay($res,100,'',['doc'=>$content]); 
		
	}
	/**
	* 获取自动续写结果
	*/
	public function get_autowrite($request,$res,$args){
		$eid = $args['eid'];
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		} 
		//验证数据 
		if(check(@$eid,'int')){
			return jsonplay($res,400,'缺少参数'); 
		}
	 	$req_id = Db::table()->get("bbx_essay", "req_id", [
			"id" => $eid
		]);
		if(empty($req_id)){
			return jsonplay($res,405,'找不到结果'); 
		}else{
			$result = Db::table()->get("bbx_autowrite", "result", [
			"req_id" => $req_id
			]);
			if(empty($result)) return jsonplay($res,101,'找不到分析结果'); 
		} 
		return jsonplay($res,100,'',json_decode($result));  
	}
	/**
	* 句式改写
	*/
	public function restate($request,$res,$args){
		$data_req = $request->getParsedBody(); 
		//验证登录
		$user=userModel::loginUser(); 
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		} 
		
		//验证数据 
		if(check(@$data_req['doc'],'required')){
			return jsonplay($res,401,'句子不能为空'); 
		}
		if(check(@$data_req['tense'],'filter',array(0,1,2,3))){
			return jsonplay($res,501,'句式参数有误');  
		}
		if(check(@$data_req['voice'],'filter',array(0,1,2,3,4,5,6,7))){
			return jsonplay($res,501,'句式参数有误');  
		}
	 
		Db::table()->insert("bbx_phrase", [
			"content" => $data_req['doc'],
			"uid" => $user['uid'],
			"ctime" => time()
		]); 
		$eid = Db::table()->id(); 
		 //推送分析
		require "AccessTokenClass.php";
		$Token = new \token();
		$result=$Token->push_restate($data_req['doc'],$data_req['tense'],$data_req['voice']); 
		if ($result['error_code']==0) {
			$key=$result['data']['key'];
			$data_a['key']=$key;
			$data_a['utime']=time();
			Db::table()->update("bbx_phrase", [
				"req_id" => $key,
				"utime" => time()
			],['id'=>$eid]); 
		}
		
		//记录数据
		$logModel=new logModel();
		$logModel->add(6,$eid); 
		return jsonplay($res,100,'',['eid'=>$eid]);  
		 
	}
	public function restate_new($request,$res,$args){
		$data_req = $request->getParsedBody(); 
		//验证登录
		$user=userModel::loginUser(); 
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		} 
		
		//验证数据 
		if(check(@$data_req['doc'],'required')){
			return jsonplay($res,401,'句子不能为空'); 
		}
		if(check(@$data_req['tense'],'filter',array(0,1,2,3))){
			return jsonplay($res,501,'句式参数有误');  
		}
		if(check(@$data_req['voice'],'filter',array(0,1,2,3,4,5,6,7))){
			return jsonplay($res,501,'句式参数有误');  
		}
		$data_to=array('doc'=>$data_req['doc'],'tense'=>$data_req['tense'],'voice'=>$data_req['voice']);
		$url="http://es.wrask.com:9334/docsim/restate";
		$return_restate=httpspost($url,json_encode($data_to),'json'); 
		return jsonplay($res,100,'',['doc'=>$return_restate]);  
		
	}
	/**
	* 获取句式改写结果
	*/
	public function get_restate($request,$res,$args){
		$eid = $args['eid'];
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		} 
		//验证数据 
		if(check(@$eid,'int')){
			return jsonplay($res,400,'缺少参数'); 
		}
		$req_id = Db::table()->get("bbx_phrase", "req_id", [
			"id" => $eid
		]);
		if(empty($req_id)){
			return jsonplay($res,405,'找不到结果'); 
		}else{
			$result = Db::table()->get("bbx_restate", "result", [
			"req_id" => $req_id
			]);
			if(empty($result)) return jsonplay($res,101,'找不到分析结果');
		}
		return jsonplay($res,100,'',json_decode($result)); 
		
	}
	/**
	* 搜索范文提交
	*/
	public function messay($request,$res,$args){
		$data_req = $request->getParsedBody(); 
		//验证登录
		$user=userModel::loginUser(); 
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		} 
		//验证数据 
		if(check(@$data_req['doc'],'required')){
			return jsonplay($res,401,'句子不能为空'); 
		} 
		Db::table()->insert("bbx_essay", [
			"content" => $data_req['doc'],
			"uid" => $user['uid'],
			"ctime" => time()
		]); 
		$eid = Db::table()->id(); 
		 //推送分析
		require "AccessTokenClass.php";
		$Token = new \token();
		$result=$Token->push_messay($data_req['doc']); 
		if ($result['error_code']==0) {
			$key=$result['data']['key'];
			$data_a['key']=$key;
			$data_a['utime']=time();
			Db::table()->update("bbx_essay", [
				"req_id" => $key,
				"utime" => time()
			],['id'=>$eid]); 
		}
		
		//记录数据
		$logModel=new logModel();
		$logModel->add(6,$eid); 
		return jsonplay($res,100,'',['eid'=>$eid]);  
	}
	/**
	* 获取句式改写结果
	*/
	public function get_messay($request,$res,$args){
		$eid = $args['eid'];
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		} 
		//验证数据 
		if(check(@$eid,'int')){
			return jsonplay($res,400,'缺少参数'); 
		}
		$req_id = Db::table()->get("bbx_essay", "req_id", [
			"id" => $eid
		]);
		if(empty($req_id)){
			return jsonplay($res,405,'找不到结果'); 
		}else{
			$result = Db::table()->get("bbx_messay", "result", [
			"req_id" => $req_id
			]);
			if(empty($result)) return jsonplay($res,101,'找不到分析结果');
		}
		return jsonplay($res,100,'',json_decode($result)); 
		
	}
	/**
	* 获取范文banner图片
	*/
	public function get_banner($req,$res,$args){ 
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		}
		$url="http://bbx.pigai.org";
		$data_res=array(
			array('img'=>$url.'/static/images/20201124113231.jpg','url'=>'other1'),
			array('img'=>$url.'/static/images/20201124113236.jpg','url'=>'class'),
			array('img'=>$url.'/static/images/20201124113240.jpg','url'=>'other2'),
		);
		return jsonplay($res,100,'',$data_res);  
	}
	/**
	 获取范文列表
	*/
	public function get_messaylist($request,$response,$args){ 
		$data_req = $request->getParsedBody();
		$p=1;
		$per=10;
		$where='';
		if(is_numeric(@$data_req['p']) && @$data_req['p']>0){
			$p=$data_req['p'];
		}
		if(@$data_req['per']>0 && @$data_req['per']<50){
			$per=$data_req['per'];
		}
		if(!check(@$data_req['tip'],'filter',array(1,2,3))){
			//$tip=$data_req['tip'];
			$where.=' and tip='.$data_req['tip'];
		}
		if(!check(@$data_req['topic'],'filter',array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18))){
			$where.=' and topic='.$data_req['topic'];
		}
		if(!check(@$data_req['genre'],'filter',array(1,2,3,4,5,6,7,8,9))){
			$where.=' and genre='.$data_req['genre'];
		}
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($response,404,'请先登录！'); 
		} 
		
		$topic_arr=$this->topic_arr;
		$tip_arr=$this->tip_arr;
		$genre_arr=$this->genre_arr;

		$result = Db::table()->query("select id,title,essay,genre,topic,tip,tags from bbx_model where 1=1 ".$where." limit ".($p-1)*$per.",".$per)->fetchAll();
		$data_res=array();
		foreach($result as $k=>$v){ 
			$data_res[$k]['tip_text']=$tip_arr[$v['tip']];
			$data_res[$k]['topic_text']=$topic_arr[$v['topic']];
			$data_res[$k]['genre_text']=$genre_arr[$v['genre']];
			if(empty($v['tags'])){
				$data_res[$k]['tags']=array();
			}else{
				$data_res[$k]['tags']=explode('|',$v['tags']);
			}
			
			$data_res[$k]['tip']=$v['tip']; 
			$data_res[$k]['title']=$v['title']; 
			$data_res[$k]['genre']=$v['genre']; 
			$data_res[$k]['topic']=$v['topic'];
			$data_res[$k]['eid']=$v['id'];
			$data_res[$k]['content']=cut_str($v['essay'],200);
		} 
		return jsonplay($response,100,'',$data_res); 
	}
	public function get_messaydetail($req,$res,$args){
		$eid = $args['eid'];
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		} 
		//验证数据 
		if(check(@$eid,'int')){
			return jsonplay($res,400,'缺少参数'); 
		}
		$essay = Db::table()->get("bbx_model", ["id","requirement","img","title","essay","genre","topic","tip","tags"],[
			"id" => $eid
		]); 
		$topic_arr=$this->topic_arr;
		$tip_arr=$this->tip_arr;
		$genre_arr=$this->genre_arr;

		if(empty($essay)){ 
			return jsonplay($res,405,'找不到结果'); 
		}else{ 
			$essay['tip_text']=$tip_arr[$essay['tip']];
			$essay['topic_text']=$topic_arr[$essay['topic']];
			$essay['genre_text']=$genre_arr[$essay['genre']]; 
			if(empty($essay['tags'])){
				$essay['tags']=array();
			}else{
				$essay['tags']=explode('|',$essay['tags']);
			}
		} 
		return jsonplay($res,100,'',$essay); 
	}
	/**
	 获取学霸范文列表
	*/
	public function get_fineslist($request,$response,$args){ 
		$data_req = $request->getParsedBody();
		$p=1;
		$per=10;
		$where='';
		if(is_numeric(@$data_req['p']) && @$data_req['p']>0){
			$p=$data_req['p'];
		}
		if(@$data_req['per']>0 && @$data_req['per']<50){
			$per=$data_req['per'];
		}
	
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		}  
	 
		$result = Db::table()->query("select id,title,origin,name,level,graduate,essay  from bbx_fines where 1=1 ".$where." limit ".($p-1)*$per.",".$per)->fetchAll();
		$data_res=array();
		foreach($result as $k=>$v){  
			$data_res[$k]['title']=$v['title']; 
			$data_res[$k]['origin']=$v['origin'];
			$data_res[$k]['name']=$v['name'];
			$data_res[$k]['level']=$v['level'];
			$data_res[$k]['graduate']=$v['graduate'];			
			$data_res[$k]['eid']=$v['id'];
			$data_res[$k]['content']=cut_str($v['essay'],200);
		} 
		return jsonplay($response,100,'',$data_res); 
	}
	public function get_finedetail($req,$res,$args){
		$eid = $args['eid'];
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		} 
		//验证数据 
		if(check(@$eid,'int')){
			return jsonplay($res,400,'缺少参数'); 
		}
		$essay = Db::table()->get("bbx_fines", ["id","requirement","img","origin","name","level","graduate","title","essay"],[
			"id" => $eid
		]); 
		if(empty($essay)){ 
			return jsonplay($res,405,'找不到结果'); 
		}
		return jsonplay($res,100,'',$essay); 
	}
	/**
	 获取名校作文列表
	*/
	public function get_famouslist($request,$response,$args){ 
		$data_req = $request->getParsedBody();
		$p=1;
		$per=10;
		$where='';
		if(is_numeric(@$data_req['p']) && @$data_req['p']>0){
			$p=$data_req['p'];
		}
		if(@$data_req['per']>0 && @$data_req['per']<50){
			$per=$data_req['per'];
		}
	
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		}  
	 
		$result = Db::table()->query("select id,title,essay,origin  from bbx_famous where 1=1 ".$where." limit ".($p-1)*$per.",".$per)->fetchAll();
		$data_res=array();
		foreach($result as $k=>$v){  
			$data_res[$k]['title']=$v['title']; 
			$data_res[$k]['origin']=$v['origin']; 			
			$data_res[$k]['eid']=$v['id'];
			$data_res[$k]['content']=cut_str($v['essay'],200);
		} 
		return jsonplay($response,100,'',$data_res); 
	}
	public function get_famousdetail($req,$res,$args){
		$eid = $args['eid'];
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		} 
		//验证数据 
		if(check(@$eid,'int')){
			return jsonplay($res,400,'缺少参数'); 
		}
		$essay = Db::table()->get("bbx_famous", ["id","requirement","img","origin","title","essay"],[
			"id" => $eid
		]); 
		if(empty($essay)){ 
			return jsonplay($res,405,'找不到结果'); 
		}
		return jsonplay($res,100,'',$essay); 
	}
	public function push_essay($request,$res,$args){ 
		$data_req = $request->getParsedBody();
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		}
		//验证数据 
		if(check(@$data_req['title'],'required')){
			return jsonplay($res,400,'题目不能为空'); 
		}
		if(check(@$data_req['content'],'required')){
			return jsonplay($res,400,'内容不能为空'); 
		}
		Db::table()->insert("bbx_essay", [
				"content" => $data_req['content'],
				"title" => $data_req['title'],
				"uid" => $user['uid'],
				"ctime" => time()
			]); 
		$eid = Db::table()->id();
		 //推送分析
		require "AccessTokenClass.php";
		$Token = new \token();
		$result=$Token->push_content($data_req['title'],$data_req['content']); 
		if ($result['error_code']==0) {
			$key=$result['data']['key'];
			$data_a['key']=$key;
			$data_a['utime']=time();
			Db::table()->update("bbx_essay", [
				"req_id" => $key,
				"utime" => time()
			],['id'=>$eid]); 
		}
		return jsonplay($res,100,'',['eid'=>$eid]); 
	}
	
	 
	public function get_alysis($req,$res,$args){
		$eid = $args['eid'];
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		} 
		//验证数据 
		if(check(@$eid,'int')){
			return jsonplay($res,400,'缺少参数'); 
		}
		$essay = Db::table()->get("bbx_essay", ["req_id","content"], [
			"id" => $eid
		]); 
		if(empty($essay)){ 
			return jsonplay($res,405,'找不到结果'); 
		}else{
			$req_id=$essay['req_id'];
			$result = Db::table()->get("bbx_alysis", ["score","score_ci",'score_ju','score_pian','score_nei','comment','sentences'], [
			"req_id" => $req_id
			]);
			if(empty($result)) return jsonplay($res,101,'找不到分析结果');
			$result['sentences']=json_decode($result['sentences'],true);
			$count_error=0;
			foreach($result['sentences'] as $v){
				foreach($v['comment'] as $vv){
					if(in_array($vv['class'],array('warn','error'))){
						$count_error++;
					}
				} 
			} 
			$result['count_error']=$count_error; 
		}
		$result['count_word']=word_count($essay['content']);
		$result['essay']=$essay['content'];
		return jsonplay($res,100,'',$result); 
	}
	/**
	*获取用户历史操作
	*/
	public function get_history($request,$response,$args){ 
		$data_req = $request->getParsedBody();
		$p=1;
		$per=10;
		if(is_numeric(@$data_req['p']) && @$data_req['p']>0){
			$p=$data_req['p'];
		}
		if(@$data_req['per']>0 && @$data_req['per']<50){
			$per=$data_req['per'];
		}
		//验证登录
		$user=userModel::loginUser();
		if(empty($user)){
			return jsonplay($res,404,'请先登录！'); 
		} 
		
		$tip_arr=array(2=>'作文批改',4=>'句子查重',5=>'相似表达',6=>'句式改写',7=>'自动续写',8=>'高分范文');
		$result = Db::table()->query("select tip,eid from bbx_log where uid=".$user['uid']." limit ".($p-1)*$per.",".$per)->fetchAll();
		$data_res=array();
		foreach($result as $k=>$v){
			if(in_array($v['tip'],array(2,4,7,8))){
				$data_res[$k]['content']=Db::table()->get("bbx_essay","content",['id'=>$v['eid']]);
			}else{
				$data_res[$k]['content']=Db::table()->get("bbx_phrase","content",['id'=>$v['eid']]);
			}
			$data_res[$k]['tip_text']=$tip_arr[$v['tip']];
			$data_res[$k]['tip']=$v['tip'];
			$data_res[$k]['eid']=$v['eid'];
		} 
		return jsonplay($response,100,'',$data_res); 
		
	}
	
	/**
	*用户登录
	*/
	public function userlogin($request,$response,$args){
		$data_req = $request->getParsedBody();  
		//验证数据 
		if(check(@$data_req['username'],'required')){
			return jsonplay($response,400,'用户名不能为空'); 
		}
		if(check(@$data_req['password'],'required')){
			return jsonplay($response,400,'密码不能为空'); 
		}
		$user=userModel::login_demo($data_req['username'],$data_req['password']); 
		if($user['code']==0){
			return jsonplay($response,100,'success',array('username'=>$data_req['username']));
		}else{
			return jsonplay($response,100+$user['code'],$user['msg']);
		}
		  
	}
	
	/**
	* 回调 结果
	*/
	public function callback_restate($req,$res,$args){
       $content=file_get_contents('php://input');
      file_put_contents(ROOT_PATH.'/cache/alysis.log',time().":".$content."input2\n",FILE_APPEND);
		//$content='{"request":{"tense":"2","voice":"2","snt":"this ia my road"},"result":["\"This road IA\""],"key":"AI124_3e372a_984a8bc6a63b4bc48ef1a4d905ffb776","_token":{"access_token":"e53b50e795acac0f32b9641cc94efd9663b5cfc0","client_id":"cdf4bd709ade93c928590825b83e372a","user_id":"124","expires":1606135117,"scope":"all_json"},"api":"restate"}';
       $arr=json_decode($content,true); 
       $data=array();
	   if($arr['api']=='restate'){
		   $data['req_id']=$arr['key'];
		   $data['result']=essay_revert(json_encode($arr['result']));
		   $data['ctime']=time();
		   Db::table()->insert("bbx_restate",$data); 
	   }elseif($arr['api']=='similarity'){
		   $data['req_id']=$arr['key'];
		   $data['result']=essay_revert(json_encode($arr['result']));
		   $data['ctime']=time();
		   Db::table()->insert("bbx_similarity",$data); 
	   }elseif($arr['api']=='parasent'){
		   $data['req_id']=$arr['key'];
		   $data['result']=essay_revert(json_encode($arr['result']));
		   $data['ctime']=time();
		   Db::table()->insert("bbx_parasent",$data); 
	   }elseif($arr['api']=='messay'){
		   $data['req_id']=$arr['key'];
		   $data['result']=essay_revert(json_encode($arr['result']));
		   $data['ctime']=time();
		   Db::table()->insert("bbx_messay",$data); 
	   }elseif($arr['api']=='autowrite'){
		   $data['req_id']=$arr['key'];
		   $data['result']=essay_revert(json_encode($arr['result']));
		   $data['ctime']=time();
		   Db::table()->insert("bbx_autowrite",$data); 
	   }else{
		   $data['score']=$arr['score'];
		   $data['score_ci']=$arr['score_cat'][1]['score'];
		   $data['score_ju']=$arr['score_cat'][2]['score'];
		   $data['score_pian']=$arr['score_cat'][3]['score'];
		   $data['score_nei']=$arr['score_cat'][4]['score'];
		   $data['comment']=$arr['comment'];
		   $data['ctime']=time();
		   $data['sentences']=json_encode($arr['sentences']);
		   $data['req_id']=$arr['key'];
		   Db::table()->insert("bbx_alysis",$data); 
	   }
       die;
    } 
	private function moveUploadedFile($directory, UploadedFile $uploadedFile){
		 $extension = pathinfo($uploadedFile->getClientFilename(), 
		 PATHINFO_EXTENSION);
		 if(empty($extension)){
           $extension='jpg';
         } 
		 $current=getdate();
		 $filename=$current['year'].$current['mon'].$current['mday'].$current['hours'].$current['minutes'].$current['seconds'];
		 $basename =$filename.rand(100,999);
		 $filename = sprintf('%s.%0.8s', $basename, $extension);
		 $uploadedFile->moveTo($directory . DIRECTORY_SEPARATOR . $filename);
		 return $filename;
	}
	public function ocr_callback($req,$res,$args){
		 echo json_encode(array('code'=>0,'msg'=>'成功'));exit;
	}
}